package com.manxi.mapper;

import com.github.pagehelper.Page;
import com.manxi.dto.GoodsSalesDTO;
import com.manxi.dto.OrdersPageQueryDTO;
import com.manxi.entity.Orders;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    /**
     * 插入一条数据
     *
     * @param order
     */
    @Insert("insert into orders (number, order_type, pickup_number, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status, amount, remark, phone, address, consignee, estimated_delivery_time, delivery_status, pack_amount, tableware_number, tableware_status)" +
            "values (#{number}, #{orderType}, #{pickupNumber}, #{status}, #{userId}, #{addressBookId}, #{orderTime}, #{checkoutTime}, #{payMethod},#{payStatus}, #{amount}, #{remark}, #{phone}, #{address}, #{consignee}, #{estimatedDeliveryTime}, #{deliveryStatus}, #{packAmount}, #{tablewareNumber}, #{tablewareStatus})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(Orders order);

    /**
     * 根据订单号查询订单
     *
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 查询当天当前时间之前堂食或自取的所卖出的订单数
     *
     * @param map
     * @return
     */
    @Select("<script>" +
            "select count(id) from orders " +
            "where (order_type = 0 OR order_type = 1) " +
            "<if test='begin != null'>AND checkout_time &gt;= #{begin} </if> " +
            "<if test='end != null'>AND checkout_time &lt;= #{end} </if> " +
            "</script>")
    Integer getPickupNumberByOrderType(Map map);

    /**
     * 修改订单信息
     *
     * @param orders
     */
    @Update("<script>" +
            "update orders set" +
            "<if test='cancelReason != null'>cancel_reason=#{cancelReason}, </if>" +
            "<if test='rejectionReason != null'>rejection_reason=#{rejectionReason}, </if>" +
            "<if test='cancelTime != null'>cancel_time=#{cancelTime}, </if>" +
            "<if test='payStatus != null'>pay_status=#{payStatus}, </if>" +
            "<if test='payMethod != null'>pay_method=#{payMethod}, </if>" +
            "<if test='deliveryTime != null'>delivery_time=#{deliveryTime}, </if>" +
            "<if test='checkoutTime != null'>checkout_time=#{checkoutTime}, </if>" +
            "<if test='status != null'>status=#{status} </if>" +
            "where id = #{id} " +
            "</script>")
    void update(Orders orders);

    /**
     * 查询历史订单
     *
     * @param ordersPageQueryDTO
     * @return
     */
    @Select("<script>" +
            "select * from orders " +
            "where 1 = 1 " +
            "<if test ='userId != null'>AND user_id = #{userId} </if> " +
            "<if test ='number != null'>AND number like CONCAT ('%',#{number},'%') </if> " +
            "<if test ='phone != null'>AND phone like CONCAT ('%',#{phone},'%') </if> " +
            "<if test ='pickupNumber != null'>AND pickup_number like CONCAT ('%',#{pickupNumber},'%') </if> " +
            "<if test ='beginTime != null'>AND order_time &gt;= #{beginTime} </if> " +
            "<if test ='endTime != null'>AND order_time &lt;= #{endTime} </if> " +
            "<if test ='status != null'>AND status = #{status} </if> " +
            "<if test ='orderType != null'>AND order_type = #{orderType} </if> " +
            "order by order_time desc " +
            "</script>"
    )
    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据id查询订单
     *
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);

    /**
     * 各个订单类型待接单的订单数量统计
     *
     * @param orderType
     */
    @Select("select count(id) from orders where order_type = #{orderType} AND status = 2")
    Integer countStatus(Integer orderType);

    /**
     * 已接单的订单数量统计
     *
     */
    @Select("select count(id) from orders where status = 3")
    Integer countReceived();

    /**
     * 根据状态和下单时间查询订单
     *
     * @param status
     * @param orderTime
     * @return
     */
    @Select("select * from orders where status = #{status} and order_time < #{orderTime}")
    List<Orders> getByStatusAndOrderTimeLT(Integer status, LocalDateTime orderTime);

    /**
     * 查询每天的营业额
     *
     * @param map
     * @return
     */
    @Select("<script> " +
            "select sum(amount) from orders " +
            "where 1 = 1 " +
            "<if test='begin != null'>AND order_time &gt;= #{begin}</if> " +
            "<if test='end != null'>AND order_time &lt;= #{end}</if> " +
            "<if test='status != null'>AND status = #{status}</if> " +
            "</script>")
    Double sumByMap(Map map);

    /**
     * 统计每天订单数量
     *
     * @param map
     * @return
     */
    @Select("<script> " +
            "select count(id) from orders " +
            "where 1 = 1 " +
            "<if test='begin != null'>AND order_time &gt;= #{begin} </if> " +
            "<if test='end != null'>AND order_time &lt;= #{end} </if> " +
            "<if test='status != null'>AND status = #{status} </if> " +
            "<if test='orderType != null'>AND order_type = #{orderType} </if> " +
            "</script>")
    Integer countByMap(Map map);

    /**
     * 查询指定时间区间内的销量排名top10
     *
     * @param begin
     * @param end
     * @return
     */
    @Select("<script> " +
            "select od.name name,sum(od.number) number from order_detail od ,orders o " +
            "where od.order_id = o.id and o.status = 5 " +
            "<if test='begin != null'>AND order_time &gt;= #{begin} </if>" +
            "<if test='end != null'>AND order_time &lt;= #{end} </if>" +
            "group by name " +
            "order by number desc " +
            "limit 0, 10" +
            "</script>")
    List<GoodsSalesDTO> getSalesTop10(LocalDateTime begin, LocalDateTime end);

    /**
     * 根据id删除订单
     *
     * @param id
     */
    @Delete("delete from orders where id = #{id}")
    void deleteById(Long id);

}
